package com.zyj.myapplication.Adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;


import com.zyj.myapplication.Activity.ClubInfoActivity;
import com.zyj.myapplication.Javabean.UserCollect;
import com.zyj.myapplication.Model.ClubModel;
import com.zyj.myapplication.Model.Constant;
import com.zyj.myapplication.R;

import java.util.List;

import it.sephiroth.android.library.picasso.Picasso;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by owner on 2017/10/13.
 */

public class CollectListAdapter extends RecyclerView.Adapter {
    private final int user_id;
    private Context mContext;
    private List mDataList;//数据存储
    private LayoutInflater mLayoutInflater;//准备布局
    private String user_name, user_photo;


    public CollectListAdapter(Context mContext, List mDataList,int user_id) {
        this.mContext = mContext;
        this.mDataList = mDataList;
        this.user_id= user_id;
        mLayoutInflater = LayoutInflater.from(mContext);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = mLayoutInflater.from(mContext).inflate(R.layout.cardview_collectlist, parent, false);
        return new ViewHolder(v);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        final UserCollect entity = (UserCollect) mDataList.get(position);
        if (null == entity)
            return;
        final ViewHolder viewHolder = (ViewHolder) holder;
        viewHolder.collect_name.setText(entity.getName());
        if (entity.getThumb().equals("")) {
            viewHolder.collect_pic.setImageResource(R.drawable.lunbo3);
        } else {
            Picasso.with(mContext).load(Constant.PIC_URL + entity.getThumb()).into(viewHolder.collect_pic);
        }
        viewHolder.collect_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ClubModel clubModel = new ClubModel();
                Call<Integer> call=clubModel.Nocollect(entity.getFan_id(),user_id);
                call.enqueue(new Callback<Integer>() {
                    @Override
                    public void onResponse(Call<Integer> call, Response<Integer> response) {
                        if (response.body() == 0) {
                            Toast.makeText(mContext, "取消关注失败", Toast.LENGTH_SHORT).show();
                        } else {
                            Toast.makeText(mContext, "取消关注成功", Toast.LENGTH_SHORT).show();
                        }

                    }

                    @Override
                    public void onFailure(Call<Integer> call, Throwable t) {

                    }
                });
            }
        });


    }

    @Override
    public int getItemCount() {
        return mDataList.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        ImageView collect_pic;
        TextView collect_name;
        Button collect_button;

        public ViewHolder(View itemView) {
            super(itemView);
            collect_button = (Button) itemView.findViewById(R.id.collect_button);
            collect_name = (TextView) itemView.findViewById(R.id.collect_name);
            collect_pic = (ImageView) itemView.findViewById(R.id.collect_pic);
        }
    }
}
